home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / C / Applications / MacGS 2.5.2ß3 / (MacGSLib.π) / gdevmac.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-04-17  |  7.9 KB  |  409 lines  |  [TEXT/R*ch]

  1. /* Copyright (C) 1993 Aladdin Enterprises.  All rights reserved.
  2.    Distributed by Free Software Foundation, Inc.
  3.  
  4. This file is part of Ghostscript.
  5.  
  6. Ghostscript is distributed in the hope that it will be useful, but
  7. WITHOUT ANY WARRANTY.  No author or distributor accepts responsibility
  8. to anyone for the consequences of using it or for whether it serves any
  9. particular purpose or works at all, unless he says so in writing.  Refer
  10. to the Ghostscript General Public License for full details.
  11.  
  12. Everyone is granted permission to copy, modify and redistribute
  13. Ghostscript, but only under the conditions described in the Ghostscript
  14. General Public License.  A copy of this license is supposed to have been
  15. given to you along with Ghostscript so you can know your rights and
  16. responsibilities.  It should be in a file named COPYING.  Among other
  17. things, the copyright notice and this notice must be preserved on all
  18. copies.  */
  19.  
  20. /* gdevmac.c */
  21. /* (color) Macintosh driver for Ghostscript library */
  22.  
  23. #include <stdio.h>
  24. #include <stddef.h>
  25. #include <Icons.h>
  26. #include "gdevmac.h"
  27. #include "gserrors.h"
  28. #include "gp_macui.h"
  29.  
  30.  
  31. extern Boolean        gHasColorQD;
  32. extern GUIProcRec    macGUIProcs;
  33.  
  34.  
  35. #define DEFAULT_DPI        72    /* Macintosh standard monitor */
  36. #define DEFAULT_WIDTH  ( 9 * DEFAULT_DPI)
  37. #define DEFAULT_HEIGHT (12 * DEFAULT_DPI)
  38.  
  39.  
  40. /* The device descriptor */
  41.  
  42. private gx_device_procs mac_procs =
  43. {
  44.     mac_open,
  45.     gx_default_get_initial_matrix,
  46.     mac_sync,
  47.     mac_output_page,
  48.     mac_close,
  49.     mac_map_rgb_color,
  50.     mac_map_color_rgb,
  51.     mac_fill_rectangle,
  52.     gx_default_tile_rectangle,
  53.     mac_copy_mono,
  54.     mac_copy_color,
  55.     mac_draw_line,
  56.     gx_default_get_bits,
  57.     gx_default_get_props,
  58.     gx_default_put_props
  59. };
  60.  
  61.  
  62. /* The instance is public. */
  63.  
  64. gx_device_mac gs_mac_device =
  65. {
  66.     sizeof (gx_device_mac),
  67.     &mac_procs,
  68.     "mac",
  69.      DEFAULT_WIDTH, DEFAULT_HEIGHT,    /* x and y extent */
  70.      DEFAULT_DPI  , DEFAULT_DPI   ,    /* x and y density */
  71.     no_margins,
  72.     dci_color (24, 255, 256),        /* # of bits per pixel,
  73.                                      * # of distinct color levels - 1,
  74.                                      * size of color cube for dithering */
  75.      0,            /* connection not initialized */
  76.  
  77.     GUI_MAGIC_NUMBER,
  78.     &macGUIProcs,
  79.  
  80.     (CWindowPtr) NULL
  81. };
  82.  
  83. /* Macros to extract colors */
  84.  
  85. #define RED(x)        ((x & 0x00FF0000) >> 8)
  86. #define GREEN(x)    ((x & 0x0000FF00))
  87. #define BLUE(x)        ((x & 0x000000FF) << 8)
  88.  
  89. /* Macro to validate arguments */
  90.  
  91. #define check_rect()                            \
  92. {                                                \
  93.     if (w <= 0 || h <= 0)                        \
  94.         return 0;                                \
  95.                                                 \
  96.     if (x < 0 || x > xdev->width  - w ||        \
  97.         y < 0 || y > xdev->height - h)            \
  98.         return_error (gs_error_rangecheck);        \
  99. }
  100.  
  101.  
  102. #define BEGIN_DRAW_PRIVATE(theEnd)                \
  103. {                                                \
  104.     short    i;                                    \
  105.     short    iEnd = (theEnd);                    \
  106.     CWindowPtr    pictWin = macPictWindow (dev);    \
  107.     void     macBeginDraw     (gx_device *dev);    \
  108.     void     macEndDraw         (gx_device *dev);    \
  109.     void     macBeginPictDraw (gx_device *dev);    \
  110.     short     macEndPictDraw   (gx_device *dev);    \
  111.                                                 \
  112.                                                 \
  113.     for (i = 0; i < iEnd; i++)                    \
  114.     {                                            \
  115.         CWindowPtr windowPtr = (i == 0)            \
  116.                                ? xdev->windowPtr\
  117.                                : pictWin;        \
  118.                                                    \
  119.                                                    \
  120.         if (i == 0)                                \
  121.             macBeginDraw (dev);                    \
  122.         else                                    \
  123.             macBeginPictDraw (dev);                \
  124.                                                 \
  125.         {
  126.  
  127. #define END_DRAW_PRIVATE                        \
  128.         }                                        \
  129.                                                 \
  130.         if (i == 0)                                \
  131.             macEndDraw (dev);                    \
  132.         else                                    \
  133.             retVal = macEndPictDraw (dev);        \
  134.     }                                            \
  135. }
  136.  
  137.  
  138. #define BEGIN_DRAW                                \
  139.     BEGIN_DRAW_PRIVATE (2)
  140.  
  141. #define END_DRAW                                \
  142.     END_DRAW_PRIVATE
  143.  
  144.  
  145. #define BEGIN_DRAW_SPECIAL(fClearPicture)        \
  146.     BEGIN_DRAW_PRIVATE (fClearPicture ? 1 : 2)
  147.  
  148.  
  149. #define END_DRAW_SPECIAL                        \
  150.     END_DRAW
  151.  
  152.  
  153. short    macCreateWindow (gx_device *dev, short defaultWidth, short defaultHeight);
  154. short    macCloseWindow  (gx_device *dev);
  155. void    macClosePicture (gx_device *dev);
  156. void    macClearPicture (gx_device *dev);
  157.  
  158.  
  159.     private int
  160. mac_open (register gx_device *dev)
  161.  
  162. {
  163.     return macCreateWindow (dev, gs_mac_device.width, gs_mac_device.height);
  164. }
  165.  
  166.  
  167. /* Close the device. */
  168.  
  169.     private int
  170. mac_close (register gx_device *dev)
  171.  
  172. {
  173.     int        retVal = macCloseWindow (dev);
  174.  
  175.  
  176.     if (retVal == 0)
  177.     {
  178.     }
  179.  
  180.     return retVal;
  181. }
  182.  
  183.  
  184. /* Synchronize the display with the commands already given */
  185.  
  186.     private int
  187. mac_sync (register gx_device *dev)
  188.  
  189. {
  190.     return 0;
  191. }
  192.  
  193.  
  194. /* Fill a rectangle with a color. */
  195.  
  196.     private int
  197. mac_fill_rectangle (register gx_device *dev,
  198.                     int x, int y, int w, int h, gx_color_index color)
  199.  
  200. {
  201.     int    retVal = 0;
  202.  
  203.  
  204.     check_rect ();
  205.  
  206.     if (color != gx_no_color_index)
  207.     {
  208.         CWindowPtr    macPictWindow (gx_device *dev);
  209.         CWindowPtr    pictWin          = macPictWindow (dev);
  210.         Boolean        fClearPicture =
  211.                         ((CWindowPeek) pictWin)->port.picSave == (Handle) NULL &&
  212.                         x == 0 && y == 0 &&
  213.                         w == xdev->width && h == xdev->height;
  214.  
  215.  
  216.         BEGIN_DRAW_SPECIAL (fClearPicture)
  217.  
  218.             Rect        theRect;
  219.  
  220.  
  221.             if (gHasColorQD)
  222.             {
  223.                 RGBColor    rgbColor;
  224.  
  225.  
  226.                 rgbColor.red   = RED   (color);
  227.                 rgbColor.green = GREEN (color);
  228.                 rgbColor.blue  = BLUE  (color);
  229.                 RGBForeColor (&rgbColor);
  230.             }
  231.  
  232.             SetRect (&theRect, x, y, x + w, y + h);
  233.             PaintRect (&theRect);
  234.             ValidRect (&theRect);
  235.  
  236.         END_DRAW_SPECIAL
  237.  
  238.         /*...Handle page erasures...*/
  239.  
  240.         if (fClearPicture)
  241.             macClearPicture (dev);
  242.     }
  243.  
  244.     return retVal;
  245. }
  246.  
  247.  
  248. /* Copy a monochrome bitmap. */
  249.  
  250.     private int
  251. mac_copy_mono (register gx_device *dev,
  252.                const unsigned char *base, int sourcex, int raster, gx_bitmap_id id,
  253.                int x, int y, int w, int h, gx_color_index zero, gx_color_index one)
  254.  
  255. {
  256.     int    retVal = 0;
  257.  
  258.  
  259.     check_rect ();
  260.  
  261.     BEGIN_DRAW
  262.  
  263.         Rect        srcRect, dstRect;
  264.         BitMap        srcBits, *dstBits;
  265.  
  266.  
  267.         if (gHasColorQD)
  268.         {
  269.             RGBColor    zeroColor;
  270.             RGBColor    oneColor;
  271.     
  272.  
  273.             zeroColor.red    = RED    (zero);
  274.             zeroColor.green = GREEN    (zero);
  275.             zeroColor.blue    = BLUE    (zero);
  276.             RGBBackColor (&zeroColor);
  277.     
  278.             oneColor.red    = RED    (one);
  279.             oneColor.green    = GREEN    (one);
  280.             oneColor.blue    = BLUE    (one);
  281.             RGBForeColor (&oneColor);
  282.         }
  283.     
  284.         SetRect (&srcRect, 0, 0, w, h);
  285.         SetRect (&dstRect, x, y, x + w, y + h);
  286.         srcBits.baseAddr      = (QDPtr) base;
  287.         srcBits.rowBytes      = raster;
  288.         srcBits.bounds.left      = srcRect.left;
  289.         srcBits.bounds.top    = srcRect.top;
  290.         srcBits.bounds.right  = srcRect.right;
  291.         srcBits.bounds.bottom = srcRect.bottom;
  292.         dstBits = &(((GrafPtr) windowPtr)->portBits);
  293.         CopyBits (&srcBits, dstBits, &srcRect, &dstRect, srcCopy, NULL);
  294.  
  295.     END_DRAW
  296.  
  297.     return retVal;
  298. }
  299.  
  300.  
  301. /* Copy a color bitmap. */
  302.  
  303.     private int
  304. mac_copy_color (register gx_device *dev,
  305.                 const unsigned char *base, int sourcex, int raster,  gx_bitmap_id id,
  306.                 int x, int y, int w, int h)
  307.  
  308. {
  309.     int    retVal = 0;
  310.  
  311.  
  312.     check_rect ();
  313.  
  314.     return retVal;
  315. }
  316.  
  317.  
  318. /* Draw a line */
  319.  
  320.     private int
  321. mac_draw_line (register gx_device *dev,
  322.                int x0, int y0, int x1, int y1, gx_color_index color)
  323.  
  324. {
  325.     int    retVal = 0;
  326.  
  327.  
  328.     if (x0 == x1 && y0 == y1)
  329.         return 0;
  330.  
  331.     if (y1 < y0)
  332.     {
  333.         register int    tmp;
  334.  
  335.  
  336.         tmp = y1; y1 = y0; y0 = tmp;
  337.         tmp = x1; x1 = x0; x0 = tmp;
  338.     }
  339.  
  340.     if (color != gx_no_color_index)
  341.     {
  342.         BEGIN_DRAW
  343.  
  344.             if (gHasColorQD)
  345.             {
  346.                  RGBColor    rgbColor;
  347.  
  348.  
  349.                 rgbColor.red   = RED   (color);
  350.                 rgbColor.green = GREEN (color);
  351.                 rgbColor.blue  = BLUE  (color);
  352.                 RGBForeColor (&rgbColor);
  353.             }
  354.  
  355.             MoveTo (x0, y0);
  356.             LineTo (x1, y1);
  357.  
  358.         END_DRAW
  359.     }
  360.  
  361.     return retVal;
  362. }
  363.  
  364.  
  365. #define COLOR_TO_CHANNEL(s)        (s & 0xFF) * (ulong) gx_max_color_value / 255
  366.  
  367.  
  368.     private gx_color_index
  369. mac_map_rgb_color(gx_device *dev, ushort r, ushort g, ushort b)
  370.  
  371. {
  372.     register gx_color_index index = (gx_color_index) 0;
  373.  
  374.  
  375.     index  = (r * 255L) / gx_max_color_value;
  376.     index <<= 8;
  377.     index |= (g * 255L) / gx_max_color_value;
  378.     index <<= 8;
  379.     index |= (b * 255L) / gx_max_color_value;
  380.  
  381.     return (index);
  382. }
  383.  
  384.  
  385.     private int
  386. mac_map_color_rgb (gx_device *dev,
  387.                    register gx_color_index color, ushort prgb[3])
  388.  
  389. {
  390.     prgb[2] = COLOR_TO_CHANNEL (color);
  391.     color >>= 8;
  392.     prgb[1] = COLOR_TO_CHANNEL (color);
  393.     color >>= 8;
  394.     prgb[0] = COLOR_TO_CHANNEL (color);
  395.     color >>= 8;
  396.  
  397.     return 0;
  398. }
  399.  
  400.  
  401.     private int
  402. mac_output_page (gx_device *dev, int num_copies, int flush)
  403.  
  404. {
  405.     macClosePicture (dev);
  406.  
  407.     return 0;    /* the page is "output" as it is drawn */
  408. }
  409.